const { app, BrowserWindow, ipcMain, Menu, MenuItem, globalShortcut } = require("electron");
const path = require("path");
const logger = require("./log.cjs");
const windowManager = require("./windowMamager.cjs");
const { isDev } = require("./utils.cjs");

let searchWin;
let searchShow = true;
function searchChatWindowInit() {
  try {
    searchWin = createWindow();
    windowManager.add("searchWin", searchWin);
    searchWin.loadURL(
      isDev()
        ? "http://localhost:5173/#/search"
        : `file://${path.join(__dirname, "../dist/index.html")}#/search`
    );
    
  } catch (error) {
    logger.error(error);
  }
}
const createWindow = () => {
  win = new BrowserWindow({
    width: 600,
    height: 147,
    center: true,
    skipTaskbar: false,
    transparent: true,
    resizable: false,
    alwaysOnTop: false,
    frame: false,
    show: true,
    autoHideMenuBar: true,
    webPreferences: {
      contextIsolation: false,
      webSecurity: false,
      //  渲染进程 开启node模块，使得JS中可以使用node的model
      nodeIntegration: true,
      // 开启 remote 模块
      enableBlinkFeatures: true,
      enableRemoteModule: true,//开启remote模块
      // 控制上下文隔离
      // contextIsolation:false,
    },
  });
  win.webContents.on("context-menu", (event, params) => {
    const menu = new Menu();
    menu.append(
      new MenuItem({
        label: "隐藏",
        click: function () {
          if (windowManager.has("searchWin")) {
            windowManager.get("searchWin").hide();
            windowManager.get("searchWin").setAlwaysOnTop(false);
          }
        },
      })
    );
    menu.append(
      new MenuItem({
        label: "关闭",
        click: function () {
          app.quit();
        },
      })
    );
    menu.popup({ window: win }); // 在指定窗口上显示菜单
  });
  handleIPC();
  // win.webContents.openDevTools();
  win.on("close", function (event) {
    app.quit();
  });

  // 事件: 还原
  win.on('restore', () => {
    // 在窗口从最小化状态还原时执行操作
    searchWin.setBounds({ width: 600, height: 540 });
  })
  win.on('blur', () => {
    //"失去焦点:blur"
    searchShow = false
  })
  return win;
};
function handleIPC() {
  ipcMain.handle("search", function (e, ...args) {
    searchWin.setBounds({ height: args[0].curHeight });
  });
  ipcMain.handle("openSetting", function (e, ...args) {
    let setWin = windowManager.get("settingWin");
    setWin.show();
    if (args[0] === 'icon') {
      win.webContents.send("iconSet", {
        iconSet: true,
      });
    }
    searchWin.hide();
  });
  ipcMain.handle("closeSearch", function (e, ...args) {
    searchWin.hide();
  });
  ipcMain.handle("closeApp", function (e, ...args) {
    app.quit();
  });
  ipcMain.handle("miniSize", function (e, ...args) {
    searchWin.minimize();
  });
  ipcMain.handle("onTop", function (e, ...args) {
    searchWin.setBounds({ y: 0 });
  });
  ipcMain.handle("setOpenKey", function (e, ...args) {
    // console.log(args[0])
    if(args[0].oldKey) globalShortcut.unregister(args[0].oldKey);
    globalShortcut.register(args[0].openKey||"Ctrl+Q", async () => {//键盘唤醒面板
      searchShow = !searchShow
      if (searchShow) {
        searchWin.setOpacity(1);
        searchWin.show();
      } else {
        searchWin.hide();
      }
    });
  });
}

module.exports = {
  searchChatWindowInit,
  searchWin,
};
